## Version Control ##
# Version 1.0, Initial Release 04/05/11  Fred Grandlienard


from PyDevice import *
from Py_Lauterbach import *
from Support.Device.Debugger_Init import *
TTB_Print = 1

## Script Init ##
def Standby(debugger):
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach: Set %s in Standby Mode" % (debugger))
		
    Cmd(debugger, "DBR_DoPracticeTimeout \"%CommonProcessorScripts\\TTB_Standby.cmm\" 10000")
    Wait(2000)
  	
def RemoveBreakpoint(debugger, *breakpoints):
    if len(breakpoints) == 0:
        Cmd(debugger, "Break.Delete /ALL")
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s - Remove All Breakpoints" % (debugger))
		
    else:
        for breakpoint in breakpoints:
            Cmd(debugger, "Break.Delete %s"%(breakpoint))	
            if TTB_Print: 
                TTB_Log(MISC, "Lauterbach: %s - Remove Breakpoint set at %s" % (debugger, breakpoint))
		
	

def SetBreakpoint(debugger, breakpoint, *options):
    command = "Break.Set %s /Program" % ( breakpoint)
    for option in options: 
        command += " %s" % (option)
    
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach: %s - Set Breakpoint at %s" % (debugger, breakpoint))
		
    Cmd(debugger, command)

def Reset(debugger):
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach: Reset %s" % (debugger))
		
    Cmd(debugger, "DBR_DoPracticeTimeout \"%CommonProcessorScripts\\TTBRESET.cmm\" 10000")
    Wait(2000)
	
		
def Cmd(debugger, command):
    if debugger == "ALL": 
        DBR_Cmd(0,command)
        DBR_Cmd(1,command)
        DBR_Cmd(2,command)
    elif debugger == "MCP1": 
        DBR_Cmd(1,command)
    elif debugger == "MCP2": 
        DBR_Cmd(2,command)
    else:
        DBR_Cmd(0,command)
		
	
def Step(debugger): 
    if debugger == "ALL": 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate == 3: 
            DBR_StepMode(0,1)
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate == 3: 
            DBR_StepMode(1,1) 
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate == 3: 
            DBR_StepMode(2,1)	
    elif debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate == 3: 
            DBR_StepMode(1,1) 
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate == 3: 
            DBR_StepMode(2,1) 
    else : 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate == 3: 
            DBR_StepMode(0,1)

			
			
def Break(debugger): 
    if debugger == "ALL": 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate == 3: 
            DBR_Break(0)
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate == 3: 
            DBR_Break(1) 
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate == 3: 
            DBR_Break(2)	
    elif debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate == 3: 
            DBR_Break(1) 
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate == 3: 
            DBR_Break(2) 
    else : 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate == 3: 
            DBR_Break(0)
     
  
def Run(debugger):
    if TTB_Print: 
        TTB_Log(MISC, "Lauterbach: Go %s" % (debugger))
		
    if debugger == "ALL": 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate != 3: 
            DBR_Go(0)
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate != 3: 
            DBR_Go(1)
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate != 3: 
            DBR_Go(2)		
    elif debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
        if  targetstate != 3: 
            DBR_Go(1)
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
        if  targetstate != 3: 
            DBR_Go(2)
    else : 
        (retc, targetstate) = DBR_GetState (0)
        if  targetstate != 3: 
            DBR_Go(0)
    Wait(2000)
	
def isAtAddress(debugger, desiredAddress):
    (retc, pp) = DBR_ReadPP(debugger)
	
    if pp == desiredAddress: 
        varName = "%s - %s %s" % (debugger, "Program Point/Breakpoint Compare", testnum)	
        TTB_Results(varName , 0, "Compares current Program Point to the desired location or breakpoint", 1, 1, 1)	
	
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s is at desired address (%s)" % (debugger, desiredAddress))	
        return 1
    else
        varName = "%s - %s %s" % (debugger, "Program Point/Breakpoint Compare", testnum)	
        TTB_Results(varName , 0, "Compares current Program Point to the desired location or breakpoint", 0, 1, 1)	
        if TTB_Print: 
            TTB_Log(MISC, "Lauterbach: %s is NOT at desired address (%s)" % (debugger, desiredAddress))	
        return 0
	


			
def isRunning(debugger):
    targetstate = 0
	
    if debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
    else : 
        (retc, targetstate) = DBR_GetState (0)

    if retc != 0: 
        return 0	
    if targetstate == 3: 
        return 1
    else:
        return 0
		
def GetState(debugger,testnum, expected):

    if debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
    else : 
        (retc, targetstate) = DBR_GetState (0)

    if retc != 0: 
		targetstate = -1
		
    varName = "%s - %s %s" % (debugger, "CPU State", testnum)	
    TTB_Results(varName , 0, "Current CPU State", targetstate, expected, expected)	
    return targetstate
	
	
def ReturnState(debugger):
    if debugger == "MCP1": 
        (retc, targetstate) = DBR_GetState (1)
    elif debugger == "MCP2" : 
        (retc, targetstate) = DBR_GetState (2)
    else : 
        (retc, targetstate) = DBR_GetState (0)

	return targetstate
	
def GetProcessorName(processor): 
    if processor == 0: 
	    return "HCP"
    elif processor == 1: 
        return "MCP1"
    elif processor == 2:
        return "MCP2"
 